package com.zhuoshi.demo;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Before;
import org.junit.Test;

import sun.misc.OSEnvironment;

import com.zhuoshi.bean.Person;
import com.zhuoshi.bean.QueryBean;

public class Demo {
	SqlSessionFactory factory;
	//每次调用 test方法的时候都会执行before
	@Before
	public void before() throws IOException{
		 InputStream is = Resources.getResourceAsStream("sqlMapConfig.xml");
		 factory = new SqlSessionFactoryBuilder().build(is);
	}
	/**
	 * 动态SQL查询
	 */
	
	@Test
	public void select(){
		SqlSession openSession = factory.openSession();
//		Map<String,Object> map = new HashMap<String,Object>();
//		map.put("name", "李");
//		map.put("age", 25);
		QueryBean bean = new QueryBean();
		//bean.setAge(25);
		bean.setName("李");
		List<Person> selectList = openSession.selectList("com.zhuoshi.personMapper.select",bean);
		for (Person person : selectList) {
			System.out.println(person);
		}
		openSession.close();
	}
	/**
	 * 动态SQL更新
	 */
	@Test
	public void update(){
		SqlSession openSession = factory.openSession();
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("name", "李仔1");
		map.put("age", 20);
		map.put("pid", 6);
		List<Person> selectList = openSession.selectList("com.zhuoshi.personMapper.update",map);
		for (Person person : selectList) {
			System.out.println(person);
		}
		openSession.close();
	}
	
	/**
	 * in 查询
	 */
	@Test
	public void selectByIn(){
		SqlSession openSession = factory.openSession();
		Map<String,Object> map = new HashMap<String,Object>();
//		List<Integer> list = new ArrayList<Integer>();
//		list.add(1);
//		list.add(2);
//		list.add(3);
//		list.add(4);
//		list.add(5);
		//Integer[] arrI = {1,2,3,4,5,6};
		String[] arrS = {"1","2","3","4","5","6","7"};
		map.put("ids",arrS);
		List<Person> selectList = openSession.selectList("com.zhuoshi.personMapper.selectByIn",map);
		for (Person person : selectList) {
			System.out.println(person);
		}
		openSession.close();
	}
	
	/**
	 * 批量插入
	 */
	@Test
	public void insertBatch(){
		SqlSession openSession = factory.openSession();
		try {
			Map<String,Object> map = new HashMap<String,Object>();
			List<Person> list = new ArrayList<Person>();
			for (int i = 0; i < 1005; i++) {
				Person p = new Person();
				p.setPname("马衍腾"+i);
				p.setAge(i-10);
				list.add(p);
				if(i%100 ==0){
					map.put("persons",list);
					openSession.insert("com.zhuoshi.personMapper.insertBatch",map);
					list.clear();
				}
			}
			map.put("persons",list);
			openSession.insert("com.zhuoshi.personMapper.insertBatch",map);
			openSession.commit();
		} catch (Exception e) {
			e.printStackTrace();
			openSession.rollback();
		} finally{
			openSession.close();
		}
	}
	
	/**
	 * 批量删除
	 */
	@Test
	public void deleteBatch(){
		SqlSession openSession = factory.openSession();
		try {
			Map<String,Object> map = new HashMap<String,Object>();
			List<Integer> list = new ArrayList<Integer>();
			for (int i = 29001; i <= 30005; i++) {
				list.add(i);
				if(i%100 ==0){
					map.put("ids",list);
					openSession.insert("com.zhuoshi.personMapper.deleteBatch",map);
					list.clear();
				}
			}
			map.put("ids",list);
			openSession.insert("com.zhuoshi.personMapper.deleteBatch",map);
			openSession.commit();
		} catch (Exception e) {
			e.printStackTrace();
			openSession.rollback();
		} finally{
			openSession.close();
		}
	}
}
